package com.scrcu.core.batch.action;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.common.Authorized;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.common.util.JsonUtil;
import com.scrcu.core.batch.biz.BatchInfoBiz;
import com.scrcu.core.batch.constant.Constants;
import com.scrcu.core.batch.entity.BatchInfo;
import com.scrcu.core.batch.service.BatchInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/info")
public class BatchInfoAction {
	@Autowired
	private BatchInfoBiz infoBiz;
	@Autowired
	private BatchInfoService infoSvc;

	@RequestMapping("/get.do")
	@Authorized(code = "batch.info.get")
	public ReplyInfo<Object> get(String id) {
		if (Util.strIsEmpty(id)) {
			return new ReplyInfo<>(Constants.WARN_INVALID, "get");
		}
		BatchInfo o = infoSvc.get(id);
		return new ReplyInfo<>(true, JsonUtil.from(o));
	}

	@RequestMapping("/start.do")
	@Authorized(code = "batch.info.start")
	public ReplyInfo<Object> start(String id, String params) {
		return infoSvc.start(id, params);
	}

	@RequestMapping("/enable.do")
	@Authorized(code = "batch.info.status")
	public ReplyInfo<Object> enable(String id) {
		return infoSvc.enable(id);
	}

	@RequestMapping("/disable.do")
	@Authorized(code = "batch.info.status")
	public ReplyInfo<Object> disable(String id) {
		return infoSvc.disable(id);
	}

	@RequestMapping("/save.do")
	@Authorized(code = "batch.info.save")
	public ReplyInfo<Object> save(@RequestBody BatchInfo o) {
		String id = infoBiz.save(o);
		return new ReplyInfo<>(true,id);
	}

	@RequestMapping("/delete.do")
	@Authorized(code = "batch.info.delete")
	public ReplyInfo<Object> delete(String id) {
		if (Util.strIsEmpty(id)) {
			return new ReplyInfo<>(Constants.WARN_INVALID, "delete");
		}
		ReplyInfo<Object> ri = this.disable(id);
		if (ri.isSuccess()) {
			infoBiz.delete(id);
			return ReplyInfo.SUCCESS;
		}
		return ri;
	}

	@RequestMapping("/list.do")
	@Authorized(code = "batch.info.list")
	public ReplyInfo<Object> list(String catalog, String key, String orderBy, int pageNo, int pageSize) {
		PagedResult<BatchInfo> pr = infoBiz.list(catalog, key, orderBy, pageNo, pageSize);
		return new ReplyInfo<>(true, pr);
	}

	/**
	 * 供子系统动态注册批量并启动
	 * 
	 * @param o
	 * @return
	 */
	@RequestMapping("/register.do")
	@Authorized(code = "batch.info.register")
	public ReplyInfo<Object> register(BatchInfo o) {
		return infoSvc.register(o);
	}

	@RequestMapping("/deregister.do")
	@Authorized(code = "batch.info.deregister")
	public ReplyInfo<Object> deregister(String id) {
		return infoSvc.deregister(id);
	}
}